// pages/sita/chat/chat.js
var chatFun = require('../../fun_js/chat.js')
var mathFun = require('../../fun_js/math.js')
var goodsFun = require('../../fun_js/goods.js')
var recordFun = require('../../fun_js/record.js')
const CONTEXT = require('../../../utils/public_var.js')
const ocr = require('../../fun_js/ocr.js')
const app = getApp()
let query = null
var windowHeight = -1
var requestURL=''
Page({
  /**
   * 页面的初始数据
   */

  data: {
    fix_height: 0,
    input_tx: '',
    intoViewName: 'endview',
    hideTip:true,
    totalCount: 0,
    hints:{},
    ocrText:'',
    payCount: 0,
    MessageList: [],
    showCrop: true,
    animationData: {},
    bar_type:CONTEXT.TEXT_PAGE_TYPE,
    PAGETYPE:CONTEXT.IMG_PAGE_TYPE,
    TSXT_MSG:CONTEXT.TEXT,
    IMG_MSG:CONTEXT.IMAGE,
    funcSupport:{},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    query = wx.createSelectorQuery().in(this);   
   
    let itemData = JSON.parse(options.itemdata)
 
    this.initPage(itemData)
 
    this.getUseCount()
    

  },
  initPage(itemData){
   
    this.setData({
      bar_type:itemData.menuName,
      funcSupport:itemData.funcSupport,
      hideTip:itemData.hints==undefined?true:false,
      hints:itemData.hints
    })
    requestURL = itemData.funCallURI
    wx.setNavigationBarTitle({
      title: itemData.menuName,
    })
    this.charBar_refresh(itemData)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },
  /**
   * 点击刷新按钮
   */
  charBar_refresh(data){

    if(data.menuID == CONTEXT.IMG_PAGE_TYPE){
      mathFun.initImgMsgData(this,data.welcomeIntro,app)
    }else{
      chatFun.initMsgData(this,data.welcomeIntro,app)
    }
  },
  showOpenTip(){
    wx.showModal({
      title: '',
      content: '我勤学苦练，\n每日更新,\n可删除并使用新版小程序',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {

        }
      }
    })
  },

  inputChangetx: function (e) {
    chatFun.inputChangetx(this, app, e)
  },
  send_new_Msg(res) {
    console.log(res)
    if(res.detail.imgList.length>0){
      //还要判断是 OCR 还是图片识别
      if(res.detail.type == "OCR"){
        if(typeof(requestURL) == "undefined"){
          var sendMsg = {
            "role": 'user',
            "content": this.data.input_tx,
            "upImgUrl":res.detail.imgList[0],
            "msgtype":CONTEXT.TEXT
          }
          var data = {
            "role": 'system',
            "content": res.detail.msg,
            "msgtype":CONTEXT.TEXT
            }
            var list = this.data.MessageList
            list.push(sendMsg)
            list.push(data)
            this.setData({
              MessageList: list
            })
            this.setData({
              intoViewName: 'endview',
            })
            this.initSendUI();
          return
        }
        console.log("OCR")
        ocr.setORCText(app,this,res.detail.msg,res.detail.imgList[0],requestURL)

      }else if(res.detail.type == "Identifyingobjects"){
        // 图文
        goodsFun.sendImgTextMsg(this,res.detail.imgList[0],app,requestURL)
      }
      
     
    }else{
      if(this.data.bar_type == "翻译"){
        wx.showToast({
          title: '请上传要识别的图片',
      
          icon:'error'
        })
        return
      }
     chatFun.sendTextMsg(this, app,requestURL)
    }
  },
   /**
   * OCR 识别成功后调用此方法
   * @param {} e 
   */
  // orcTextMsg(e){
  //   // ocr.setORCText(app,this,e.detail.data,requestURL)
  //   this.data.ocrText = e.detail.data
  // },
  sendUpImgMsg(){
    goodsFun.uploadImg(this,app)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  
  },
  setScrollViewHeight(height) {
    var that = this
    if (windowHeight > 0) {
      this.initHeight(height)
    } else {
      wx.getSystemInfo({
        success: function (res) {

          windowHeight = res.windowHeight; // 获取页面可视区域的高度

          that.initHeight(height)
        }
      });
    }
  },
  initHeight(height) {

    var that = this
    that.setData({
      fix_height: windowHeight - height
    })
    setTimeout(()=>{
      that.setData({
        intoViewName: 'endview'
      })
    },100)
    
  },
  getUseCount() {
    var that = this
    wx.request({
      url: app.app_url.BASE_URL + app.app_url.SITA_USE_COUNT,
      method: 'POST',
      data: {
        'openid': app.globalData.OpenID
      },
      success(res) {

        if (res.data.status == 400 || res.data.status == 200) {
          // that.data.totalCount = 100
          //  that.data.totalCount = res.data.total
           that.setData({
            totalCount:res.data.total
           })
        }
      },
      fail(res) {
        console.log(res)
      }

    })
  },
  bindList(){
    this.setData({
      showCrop:true
    })
    const child = this.selectComponent('#bottom');
    child.hintTip();
  },
  
  charBarInputLineChange(e) {
  
    var height = e.detail.data
    this.setScrollViewHeight(height)
  },
  /**
   * 上传图片
   */
  upload(){
    mathFun.upload(this,app)
  },
  /**
   * 剪裁图片
   */
  editImg(){
    mathFun.editImg(this,app,requestURL)

  },
  /**
   * 获取录音文件
   */
  getRecordFile(e){
    console.log("获取录音文件");
    console.log(e);
   var text = e.detail.detail
   recordFun.ocrRequest(this,app,requestURL,text)
  },
 
  hintAll(){
    const child = this.selectComponent('#bottom');
    child.hintAll();
  },
  initSendUI(){
    const child = this.selectComponent('.bottom_class');
    child.initInputTx();
  }


})